<?php
/**
 * 后台管理端核销人员申请
 * Class CouponAfterScan
 */
namespace Scan;

use Model\AfterPersonMod;

class AdminAfterApplyScan extends Scan
{
    private $AfterPerson = null;   //活动员工核销

    private $_after_person = array();  //数据

    public function __construct()
    {
        parent::__construct();
        $this->AfterPerson = new AfterPersonMod();
        $this->_after_person = $this->AfterPerson->findOne(array('activity_id'=>$this->_activity_id,'uid'=>$this->_scan_uid),array('id','status'));
    }

    public function index()
    {
        $this->_result = array('status'=>'error','err_msg'=>'申请核销员工失败，请重试');
        if(!$this->_after_person){
            if($this->add()) $this->_result = array('status'=>'success','success_msg'=>'申请核销员工成功，等待审核');
        }else if($this->_after_person['status'] > 2){
            if($this->edit()) $this->_result = array('status'=>'success','success_msg'=>'申请核销员工成功，等待审核');
        }else{
            if($this->_after_person['status'] == 1){
                $this->_result = array('status'=>'error','err_msg'=>'您已经是该活动的核销员工了');
            }else{
                $this->_result = array('status'=>'error','err_msg'=>'已经申请核销资格了，请耐心等待审核');
            }
        }
        return $this->_result;
    }

    /**
     * 新增核销人员
     */
    private function add()
    {
        return $this->AfterPerson->insert(array(
            'uniacid'=>$this->_uniacid,
            'activity_id'=>$this->_activity_id,
            'uid'=>$this->_scan_uid
        ));
    }

    /**
     * 修改核销人员
     */
    private function edit()
    {
        return $this->AfterPerson->update(array('status'=>2),array('id'=>$this->_after_person['id']));
    }
}
